import { ref } from 'vue';
import { getAutoFillExpContent, getFormColType, listSource } from '@/api/web/bu/form';

export function useOptions() {
  const formatOptions = ref([
    { label: '短日期（yyyy-MM-dd）', value: 'yyyy-MM-dd', type: 'DatePicker' },
    { label: '短日期（yyyy/MM/dd）', value: 'yyyy/MM/dd', type: 'DatePicker' },
    { label: '长日期（yyyy年MM月dd日）', value: 'yyyy年MM月dd日', type: 'DatePicker' },
    { label: 'HH:mm', value: 'HH:mm', type: 'TimePicker' },
    { label: 'HH:mm:ss', value: 'HH:mm:ss', type: 'TimePicker' },
    { label: 'yyyy-MM-dd HH:mm:ss', value: 'yyyy-MM-dd HH:mm:ss', type: 'DatetimePicker' },
    { label: 'yyyy/MM/dd HH:mm:ss', value: 'yyyy/MM/dd HH:mm:ss', type: 'DatetimePicker' },
    {
      label: 'yyyy年MM月dd日 HH时mm分ss秒',
      value: 'yyyy年MM月dd日 HH时mm分ss秒',
      type: 'DatetimePicker',
    },
  ]);

  const xzqhOptions = ref([
    { label: '区', value: '区' },
    { label: '街道', value: '街道' },
    { label: '社区', value: '社区' },
    { label: '网格', value: '网格' },
    { label: '小区', value: '小区' },
    { label: '楼栋', value: '楼栋' },
    { label: '房屋', value: '房屋' },
  ]);

  const listStatusOptions = ref([
    { label: '正常', value: '正常' },
    { label: '隐藏', value: '隐藏' },
    { label: '内部链接', value: '内部链接' },
    { label: '内部弹窗', value: '内部弹窗' },
    { label: '外部链接', value: '外部链接' },
  ]);

  const statusOptions = ref([
    { label: '正常', value: '正常' },
    { label: '隐藏', value: '隐藏' },
    { label: '只读', value: '只读' },
    { label: '自动填充', value: '自动填充' },
    { label: '自动填充且只读', value: '自动填充且只读' },
    { label: '自动填充且隐藏', value: '自动填充且隐藏' },
  ]);

  const controlOptions = ref([
    { label: '单行输入框', value: 'input' },
    { label: '多行输入框', value: 'textarea' },
    { label: '密码框', value: 'password' },
    { label: '单选框', value: 'radio' },
    { label: '复选框', value: 'checkbox' },
    { label: '下拉框', value: 'select' },
    { label: '日期', value: 'DatePicker' },
    { label: '时间', value: 'TimePicker' },
    { label: '日期时间', value: 'DatetimePicker' },
    { label: '文件', value: 'FileUpload' },
    { label: '图片', value: 'ImageUpload' },
    { label: '视频', value: 'VideoUpload' },
  ]);

  function formatControlType(row, { property }) {
    const v = row[property];
    return controlOptions.value.filter((k) => k.value === v)?.[0]?.label ?? '';
  }

  const validatorOptions = ref([
    { label: '邮件', value: '邮件' },
    { label: '手机号', value: '手机号' },
    { label: '身份证号', value: '身份证号' },
    { label: '港澳通行证', value: '港澳通行证' },
    { label: '字母', value: '字母' },
    { label: '数字', value: '数字' },
  ]);

  const autoFillExpList = ref({
    当前时间: 'Date',
    用户id: 'userInfo.userId',
    用户名: 'userInfo.username',
    用户所属区: 'userInfo.userXzqhQu',
    用户所属小区: 'userInfo.userXzqhXq',
    用户所属社区: 'userInfo.userXzqhSq',
    用户所属网格: 'userInfo.userXzqhWg',
    用户所属街道: 'userInfo.userXzqhJd',
    用户手机号: 'userInfo.userPhone',
    用户姓名: 'userInfo.userNickName',
    用户证件号: 'userInfo.userIdno',
    部门id: 'userInfo.deptId',
    部门名称: 'userInfo.deptName',
  });

  function formatAutoFillExpWeb(row, { property }) {
    const v = row[property];
    return Object.entries(autoFillExpList.value).filter((k) => k[1] === v)?.[0]?.[0] ?? '';
  }

  const queryOptions = ref([
    { label: '不设为查询条件', value: 'not' },
    { label: '等值查询', value: 'eq' },
    { label: '模糊匹配', value: 'like' },
    { label: '多条件等值', value: 'in' },
    { label: '大于', value: 'gt' },
    { label: '大于等于', value: 'gte' },
    { label: '小于', value: 'lt' },
    { label: '小于等于', value: 'lte' },
    { label: '范围查询', value: 'range' },
    { label: 'findInSet', value: 'findInSet' },
  ]);

  function formatQueryCondition(row, { property }) {
    const v = row[property];
    return queryOptions.value.filter((k) => k.value === v)?.[0]?.label ?? '';
  }

  const idStrategyOptions = ref([
    { label: '序列', value: '序列' },
    { label: '自增', value: '自增' },
    { label: '自增uid', value: '自增uid' },
  ]);

  const dataScopeTypeOptions = ref({
    不限制: 'noLimit',
    按行政区划: 'byXzqh',
    按部门: 'byDept',
  });

  const supportedDesensitizationObj = ref({
    defaultIdnoDesensitizer: '默认证件号或手机号脱敏器',
    defaultNameDesensitizer: '默认姓名脱敏器',
  });

  function formatDesensitization(row) {
    const { desensitizationType } = row;
    return supportedDesensitizationObj.value[desensitizationType] || '';
  }

  return {
    formatOptions,
    xzqhOptions,
    listStatusOptions,
    statusOptions,
    controlOptions,
    formatControlType,
    validatorOptions,
    autoFillExpList,
    formatAutoFillExpWeb,
    queryOptions,
    formatQueryCondition,
    idStrategyOptions,
    dataScopeTypeOptions,
    supportedDesensitizationObj,
    formatDesensitization,
  };
}

export function useAutoFillExpOptions() {
  const autoFillExpOptions = ref([]);

  getAutoFillExpContent().then(({ data }) => {
    autoFillExpOptions.value = Object.keys(data).map((k) => ({
      label: k,
      value: data[k],
    }));
  });

  function formatAutoFillExp(row, { property }) {
    const v = row[property];
    return autoFillExpOptions.value.filter((k) => k.value === v)?.[0]?.label ?? '';
  }

  return {
    autoFillExpOptions,
    formatAutoFillExp,
  };
}

export function useFieldOptions() {
  const fieldOptions = ref([]);

  getFormColType().then(({ data }) => {
    fieldOptions.value = Object.keys(data).map((k) => ({
      label: k,
      value: data[k],
    }));
  });

  function formatFieldType(row, { property }) {
    const v = row[property];
    return fieldOptions.value.filter((k) => k.value === v)?.[0]?.label ?? '';
  }

  return {
    fieldOptions,
    formatFieldType,
  };
}

export function useDataSourceIdOptions() {
  const dataSourceIdOptions = ref([]);

  listSource().then(({ data = [] }) => {
    dataSourceIdOptions.value = data.map((k) => ({
      value: k.id,
      label: k.name,
    }));
  });

  return {
    dataSourceIdOptions,
  };
}
